import * as vscode from 'vscode';

export class MyTreeDataProvider implements vscode.TreeDataProvider<vscode.TreeItem> {
    private _onDidChangeTreeData: vscode.EventEmitter<vscode.TreeItem | undefined | void> = new vscode.EventEmitter<vscode.TreeItem | undefined | void>();
    readonly onDidChangeTreeData: vscode.Event<vscode.TreeItem | undefined | void> = this._onDidChangeTreeData.event;

    constructor() {}

    refresh(): void {
        this._onDidChangeTreeData.fire();
    }

    getTreeItem(element: vscode.TreeItem): vscode.TreeItem {
        // 添加命令以在工作区打开文件或网站
        if (element.command === undefined) {
            if (element.label === "Sub Item 1.1") {
                element.command = {
                    command: 'vscode.open',
                    title: 'Open URL',
                    arguments: [vscode.Uri.parse('https://developer.mozilla.org')] // 修改为开发者工具网站链接
                };
            } else if (element.label === "Sub Item 1.2") {
                element.command = {
                    command: 'vscode.open',
                    title: 'Open URL',
                    arguments: [vscode.Uri.parse('https://github.com')] // 修改为开发者工具网站链接
                };
            } else if (element.label === "Sub Item 2.1") {
                element.command = {
                    command: 'vscode.open',
                    title: 'Open URL',
                    arguments: [vscode.Uri.parse('https://example.com/subitem21')]
                };
            } else if (element.label === "Sub Item 2.2") {
                element.command = {
                    command: 'vscode.open',
                    title: 'Open URL',
                    arguments: [vscode.Uri.parse('https://example.com/subitem22')]
                };
            } else {
                element.command = {
                    command: 'vscode.open',
                    title: 'Open File',
                    arguments: [vscode.Uri.file(element.label as string)]
                };
            }
        }
        return element;
    }

    getChildren(element?: vscode.TreeItem): Thenable<vscode.TreeItem[]> {
        if (element) {
            // 如果有子元素，返回子元素
            if (element.label === "Item 1") {
                return Promise.resolve([
                    new vscode.TreeItem("Sub Item 1.1", vscode.TreeItemCollapsibleState.None),
                    new vscode.TreeItem("Sub Item 1.2", vscode.TreeItemCollapsibleState.None)
                ]);
            } else if (element.label === "Item 2") {
                return Promise.resolve([
                    new vscode.TreeItem("Sub Item 2.1", vscode.TreeItemCollapsibleState.None),
                    new vscode.TreeItem("Sub Item 2.2", vscode.TreeItemCollapsibleState.None)
                ]);
            }
            return Promise.resolve([]);
        } else {
            // 返回顶级元素
            return Promise.resolve([
                new vscode.TreeItem("Item 1", vscode.TreeItemCollapsibleState.Collapsed),
                new vscode.TreeItem("Item 2", vscode.TreeItemCollapsibleState.Collapsed)
            ]);
        }
    }
}